<% 
local sx1301_nb = 0;

self.map.uci:foreach("lora_pkt_fwd","sx1301",
function(sec)
sx1301_nb = sx1301_nb + 1;
end
)

%>

<script type="text/javascript">
	var SX1301_NUM = <%=sx1301_nb%>
	var MAX_FREQ_NUM = 8*SX1301_NUM;

	var EU868_DEF_FREQS = [868.1,868.3,868.5];
	var EU868_FREQ_MIN = 863;
	var EU868_FREQ_MAX = 870;
	var EU868_STD_FREQ_MIN = 863;
	var EU868_STD_FREQ_MAX = 870;
	var EU868_STD_FREQ_STEP = 1.6
	var EU868_STD_SF = 7
	var EU868_STD_BW = 250
	var EU868_FSK_DATARATE = 50
	var EU868_TX_FREQ = [863, 870]; 

	var IN865_DEF_FREQS = [865.0625,865.4025,865.9850];
	var IN865_FREQ_MIN = 865;
	var IN865_FREQ_MAX = 867;
	//var IN865_STD_FREQ_MIN = 900;
	//var IN865_STD_FREQ_MAX = 930;
    //var IN865_STD_FREQ_STEP = 1.6
    var IN865_FSK_DATARATE = 50
    var IN865_TX_FREQ = [865, 867]; 

    var KR920_DEF_FREQS = [922.1,922.3,922.5];
    var KR920_FREQ_MIN = 920;
    var KR920_FREQ_MAX = 923;    
	//var KR920_STD_FREQ_MIN = 900;
	//var KR920_STD_FREQ_MAX = 930;
    //var KR920_STD_FREQ_STEP = 1.6
    //var KR920_FSK_DATARATE = 50
    var KR920_TX_FREQ = [920.9, 923.3]; 

    var AS923_DEF_FREQS = [923.2, 923.4];
    var AS923_FREQ_MIN = 900;
    var AS923_FREQ_MAX = 930;
    var AS923_STD_FREQ_MIN = 900;
    var AS923_STD_FREQ_MAX = 930;
    var AS923_STD_FREQ_STEP = 1.6;
    var AS923_STD_SF = 7;
    var AS923_STD_BW = 250;
    var AS923_FSK_DATARATE = 50;
    var AS923_TX_FREQ = [920, 925]; 

    var EU433_DEF_FREQS = [433.175,433.375,433.575];
    var EU433_FREQ_MIN = 433.175;
    var EU433_FREQ_MAX = 434.665;
    var EU433_STD_FREQ_MIN = 433.175;
    var EU433_STD_FREQ_MAX = 434.665;
    var EU433_STD_FREQ_STEP = 1.6
    var EU433_STD_SF = 7;
    var EU433_STD_BW = 250;
    var EU433_FSK_DATARATE = 50;
    var EU433_TX_FREQ = [433, 435];

    var US915_DEF_FREQS = [];
    var US915_FREQ_MIN = 902.3;
    var US915_FREQ_MAX = 914.9;
    var US915_FREQ_STEP = 0.2;
    var US915_STD_FREQ_MIN = 903.0;
    var US915_STD_FREQ_MAX = 914.2;
    var US915_STD_FREQ_STEP = 1.6;
    var US915_STD_SF = 8;
    var US915_STD_BW = 500;
	//var US915_FSK_DATARATE = 50
	var US915_TX_FREQ = [902, 928];


	var AU915_DEF_FREQS = [];
	var AU915_FREQ_MIN = 915.2;
	var AU915_FREQ_MAX = 927.8;
	var AU915_FREQ_STEP = 0.2;
	var AU915_STD_FREQ_MIN = 915.9;
	var AU915_STD_FREQ_MAX = 927.1;
	var AU915_STD_FREQ_STEP = 1.6;
	var AU915_STD_SF = 8;
	var AU915_STD_BW = 500;
	//var AU915_FSK_DATARATE = 50
	var AU915_TX_FREQ = [915, 928];

	var CN470_DEF_FREQS = [];
	var CN470_FREQ_MIN = 470.3;
	var CN470_FREQ_MAX = 489.3;
	var CN470_FREQ_STEP = 0.2;
	//var CN470_STD_FREQ_MIN = 0;
	//var CN470_STD_FREQ_MAX = 0;
	//var CN470_STD_FREQ_STEP = 0;
	//var CN470_FSK_DATARATE = 50
	var CN470_TX_FREQ  = [470, 510];

	var RU864_DEF_FREQS = [868.9, 869.1]
	var RU864_FREQ_MIN = 864.1;
    var RU864_FREQ_MAX = 869.9;
    var RU864_STD_FREQ_MIN = 864.2;
    var RU864_STD_FREQ_MAX = 869.8;
    var RU864_STD_FREQ_STEP = 1.6
    var RU864_STD_SF = 7;
    var RU864_STD_BW = 250;
    var RU864_FSK_DATARATE = 50;
    var RU864_TX_FREQ = [864, 870];

	function frequency2channenId(f, region){
		var freq_start = eval(region+"_FREQ_MIN") * 1000000;
		var freq_step = eval(region+"_FREQ_STEP")* 1000000;
		var freq = f * 1000000;

		if(( freq - freq_start )%freq_step != 0){
			return -1;
		}
		return ( freq - freq_start ) / freq_step;
	}

	function clear_radio_params(){
		for( var i = 0; i < <%=sx1301_nb%>; i++ ){

			$("#_"+i+"_disabled").val(1);
			$("#_"+i+"_radio0_enable").val(0);
			$("#_"+i+"_radio1_enable").val(0);
			$("#_"+i+"_std_enable").val(0); 
			$("#_"+i+"_std_if").val(0);
			$("#_"+i+"_std_radio").val(0);
			$("#_"+i+"_std_sf").val(7);
			$("#_"+i+"_std_bw").val(125000);
			$("#_"+i+"_fsk_enable").val(0); 
			$("#_"+i+"_fsk_if").val(0);
			$("#_"+i+"_fsk_radio").val(0);
			$("#_"+i+"_fsk_bps").val(50000);
			for( var j = 0; j < 8 ; j++){
				$("#_"+i+"_ch"+j+"_enable").val(0);
				$("#_"+i+"_ch"+j+"_if").val(0);
				$("#_"+i+"_ch"+j+"_radio").val(0);
			}
		}
	}
	function caculate_radio_params(){
		
		clear_radio_params();
		var freqs = [];
		
		var o_freqs = get_current_freqs();
		for( var i = 0; i < o_freqs.length; i++ ){
			freqs.push(o_freqs[i]);
		}
		
		var stds = get_current_stds();
		var fsks = get_current_fsks();

		for( var i = 0; i < stds.length; i++){
			freqs.push(stds[i]);
		}

		for( var i = 0; i < fsks.length; i++ ){
			freqs.push(fsks[i]);
		}

		freqs.sort(function(a,b){
			return a.freq - b.freq;
		});

		console.log("freqs",freqs);

		var size = freqs.length;
		var index = 0;
		var r_arr = [];
		var r_arr_idx = 0;
		var r_id = 0;
		var ch_id = 0;
		var sx1301_id = 0;
		var pre_ch_id = 0;

		while( index < size ){
			console.log("index = " + index);

			var pre_freq = freqs[index];
			var b_flag = false;
			if( r_arr_idx == 0 
				|| ((pre_freq.type == "multiSF") && ( Math.abs(r_arr[0].freq*1000000 - pre_freq.freq*1000000) <= 800000 ))
				|| ((pre_freq.type == "fsk" || pre_freq.type == "std") && ( Math.abs(r_arr[0].freq*1000000 - pre_freq.freq*1000000) <= 700000 ))
				)
			{
				if( pre_freq.type != "multiSF" || pre_ch_id < 8 ){
					console.log("push channel to r_arr", pre_freq);
					r_arr[r_arr_idx++] = pre_freq;
					index ++;
					if( pre_freq.type == "multiSF"){
						pre_ch_id ++;
					}
				}
				else{

					b_flag = true
				}
			}
			

			if(( index == size) || /*最后一个频点了 */ 
				(r_arr_idx == 5) /* 当前射频已经满了 */
				|| (r_arr_idx != 0 && ( Math.abs(r_arr[0].freq*1000000 - pre_freq.freq*1000000) > 800000 ))/*频率超过当前radio的可配置范围 */
				|| b_flag
				){
				var sum = 0;
			for(var i = 0; i < r_arr_idx ;i++){
				sum += r_arr[i].freq*1000000;
			}

			var r_center = Math.round(sum / r_arr_idx );
			
			if( Math.abs(r_arr[r_arr_idx - 1].freq * 1000000 - r_center) > 300000 && r_arr[r_arr_idx - 1].type != "multiSF"){
				r_center += Math.abs((r_arr[0].type == "multiSF"?400000:300000)) - Math.abs(r_center - r_arr[0].freq * 1000000);
			}

			if( Math.abs(r_arr[0].freq*1000000 - r_center) > 300000 && (r_arr[0].type != "multiSF")){
                r_center -= Math.abs( (r_arr[r_arr_idx - 1].type == "multiSF"?400000:300000) - Math.abs(r_center - r_arr[r_arr_idx - 1].freq*1000000));
            }

			console.log("SX1301-"+sx1301_id+" Radio"+r_id+" center freq = " + r_center);
			$("#_"+sx1301_id + "_radio"+r_id+"_enable").val(1);
			$("#_" + sx1301_id + "_disabled").val(0);
			$("#_" + sx1301_id + "_radio"+r_id+"_center_freq").val(r_center);

			for( var j = 0; j < r_arr_idx; j++){
				if( r_arr[j].type == "multiSF" ){
					var channel =  "_" + sx1301_id + "_ch" + (ch_id++);
					var channel_if = r_arr[j].freq*1000000 - r_center;
					$("#" + channel + "_enable").val("1");
					$("#" + channel + "_radio").val(r_id);
					$("#" + channel + "_if").val(channel_if);

					console.log(channel + " radio=" + r_id + " if="+channel_if);
				}
				else if( r_arr[j].type == "std" ){
					var channel =  "_" + sx1301_id + "_std";
					var channel_if = r_arr[j].freq*1000000 - r_center;
					$("#" + channel + "_enable").val("1");
					$("#" + channel + "_radio").val(r_id);
					$("#" + channel + "_if").val(channel_if);
					$("#" + channel + "_sf").val(r_arr[j].sf);
					$("#" + channel + "_bw").val(r_arr[j].bw);
					console.log(channel + " radio=" + r_id + " if="+channel_if);


				}else if( r_arr[j].type == "fsk"){
					var channel =  "_" + sx1301_id + "_fsk";
					var channel_if = r_arr[j].freq*1000000 - r_center;
					$("#" + channel + "_enable").val("1");
					$("#" + channel + "_radio").val(r_id);
					$("#" + channel + "_if").val(channel_if);
					$("#" + channel + "_bps").val(r_arr[j].dr*1000);
					console.log(channel + " radio=" + r_id + " if="+channel_if);
				}
			}
			r_id++;
			r_arr_idx = 0;
			r_arr = [];
			if( r_id == 2){
				r_id = 0;
				sx1301_id ++;
				ch_id = 0;
				pre_ch_id = 0;

			}
		}

		if( sx1301_id == SX1301_NUM)
			break;
		if( index == size )
			break;
	}
}


function init_freqlist(freqs){
	var content = ""
	for( var i = 0 ; i < freqs.length; i++){
		content += '<div><input  value="'+freqs[i].freq+'MHz" name="cbid.lora_pkt_fwd.freq_plan.freq" type="text" readonly required class="cbi-input-text"><i onclick="remove_freq(this)" class="fa fa-close"></i></div>';
	}

	if( content.length == 0 ){
		content += '<div><input required="required"  value="" name="cbid.lora_pkt_fwd.freq_plan.freq" type="text" readonly class="cbi-input-text"></div>';
	}

	$("#freq-list-div").html(content);
}

function init_fsk_list(fsks){
	var content = ""
	for( var i = 0; i < fsks.length; i++ ){
		content += '<div><input class="cbi-input-text" value="'+fsks[i].freq + 'MHz ' + fsks[i].dr + 'Kbps" readonly type="text" class="cbi-input-text" name="cbid.lora_pkt_fwd.freq_plan.fsk" /><i onclick="remove_fsk_freq(this)" class="fa fa-close"></i></div>';
	}
	if( content.length == 0 ){
		content += '<div><input class="cbi-input-text" value="" readonly type="text" class="cbi-input-text" name="cbid.lora_pkt_fwd.freq_plan.fsk" /></div>';
	}
	$("#fsk-freq-list-div").html(content);
}

function init_lora_list(stds){
	var content = ""
	for( var i = 0; i < stds.length; i++ ){
		content += '<div><input class="cbi-input-text" value="'+stds[i].freq + "MHz SF" + stds[i].sf + ' BW' + stds[i].bw + '" readonly type="text" class="cbi-input-text" name="cbid.lora_pkt_fwd.freq_plan.lora_std" /><i onclick="remove_fsk_freq(this)" class="fa fa-close"></i></div>';
	}

	if( content.length == 0 ){
		content += '<div><input class="cbi-input-text" value="" readonly type="text" class="cbi-input-text" name="cbid.lora_pkt_fwd.freq_plan.lora_std" /></div>';
	}

	$("#std-freq-list-div").html(content);
}

function get_current_freqs(){
	var freqs = [];
	var cur_freqs_node = $("input[name='cbid.lora_pkt_fwd.freq_plan.freq']");
	cur_freqs_node.each(function(){
		if( this.value != ""){
			var freq =  parseFloat(this.value);
			freqs.push( { freq:freq, type : "multiSF"});
		}
		
	});

	return freqs;
}

function get_current_stds(){
	var stds = $("input[name='cbid.lora_pkt_fwd.freq_plan.lora_std']");
	var std_arr = [];
	for( var i = 0; i < stds.length; i++ ){
		var std_str = stds[i].value;
		if( std_str == "" )
			continue;
		var std_param_arr = std_str.split(' ');
		var std_freq = parseFloat(std_param_arr[0]);
		var std_sf = parseInt(std_param_arr[1].substring(2));
		var std_bw = parseInt(std_param_arr[2].substring(2)) * 1000;
		if( std_freq > 0)
			std_arr.push({freq : std_freq, type : "std", sf : std_sf, bw : std_bw});
	}

	return std_arr;
}

function get_current_fsks(){
	var fsks = $("input[name='cbid.lora_pkt_fwd.freq_plan.fsk']");
	var fsk_arr = [];

	for( var i = 0; i < fsks.length; i++ ){
		var fsk_str = fsks[i].value;
		if( fsk_str == "" )
			continue;
		var fsk_param_arr = fsk_str.split(' ')
		var fsk_freq = parseFloat(fsk_param_arr[0]);
		var fsk_dr = parseInt(fsk_param_arr[1]);
		if( fsk_freq > 0)
			fsk_arr.push({freq : fsk_freq, type : "fsk", dr : fsk_dr } );
	}

	return fsk_arr;
}

function remove_freq(item){
	var rm_freq = parseFloat($(item).prev().val());
	var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();
	var cur_freqs = get_current_freqs();
	var def_freqs = eval(region + "_DEF_FREQS");
	for( var i = 0; i < def_freqs.length; i++ ){
		if( rm_freq == def_freqs[i] ){
			alert("<%:Cannot remove the default channel!%>");
			return;
		}
	}
	for(var i = 0; i < cur_freqs.length; i++ )
	{
		if( cur_freqs[i].freq == rm_freq ){
			cur_freqs.splice(i,1);
		}
	}
		//$(item).parent().remove();
		init_freqlist(cur_freqs);
		caculate_radio_params();
	}
	function add_freq(f){
		var cur_freqs = get_current_freqs();
		if( cur_freqs.length >= MAX_FREQ_NUM ){
			return;
		}

		var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();
		if( f < eval(region + "_FREQ_MIN") || f > eval(region + "_FREQ_MAX"))
		{
			alert("<%:Out of the frequency range of region%>" + " " + region );
			return;
		}

		if( region == "CN470" || region == "AU915" || region == "US915" ){
			var channel_id = frequency2channenId(f, region);
			if( channel_id < 0 ){
				alert(f + " is not a valid channel. The calculation formula is:\n freq = " + eval(region + "_FREQ_MIN") + " + channelID * " + eval(region+"_FREQ_STEP"));
				return;
			}
		}

		if( cur_freqs.length > 0){
			for( var i = 0; i < cur_freqs.length;i++){
				if( cur_freqs[i].freq == f ){
					alert("<%:This channel already exists!%>");
					return;
				}
				if( Math.abs(cur_freqs[i].freq*1000000 - f*1000000) < 200000 ){
					console.log(Math.abs(cur_freqs[i].freq*1000000 - f*1000000 ) );
					if(!confirm("The difference between " + f + " and existing channel "+cur_freqs[i]+" is less than 200KHz. Are you sure to add this frequency?")){
						return
					}
				}
			}
			
			if( f < cur_freqs[0].freq && (cur_freqs[cur_freqs.length-1].freq - f ) > MAX_FREQ_NUM * 0.2 ){
				alert("The difference from the existing channel can not exceed "+MAX_FREQ_NUM * 0.2+"MHz");
				return;
			}

			if( f > cur_freqs[cur_freqs.length-1].freq && (f - cur_freqs[0].freq ) > MAX_FREQ_NUM * 0.2 ){
				alert("The difference from the existing channel can not exceed "+MAX_FREQ_NUM * 0.2+"MHz");
				return;
			}
		}
		cur_freqs.push({freq : f, type : "multiSF"});
		cur_freqs.sort(function(a,b){
			return a.freq - b.freq;
		});

		init_freqlist(cur_freqs);
		caculate_radio_params();
		var domf;
		$("input[name='cbid.lora_pkt_fwd.freq_plan.freq']").each(function(){
			if( parseFloat($(this).val()) == f  ){
				domf = this;
			}
		});

		$(domf).addClass("new");
		setTimeout(function(){
			$(domf).removeClass("new");
		},200);

	}

	function add_std_freq(){
		var f = $("#std-freq-new").val();
		var dr = $("#std-sf-new").val();
		var bw = $("#std-bw-new").val();
		var cur_freqs = get_current_freqs();
		var cur_stds = get_current_stds();

		$("std-freq-new").val("");
		
		if( cur_stds.length >= SX1301_NUM ){
			return;
		}

		for(var i = 0; i < cur_stds.length; i++ ){
			if( Math.abs(cur_stds[i].freq * 1000000 - f*1000000) < 1.6*1000000 ){
				alert("The difference between the new channel's frequency and the existing channel's frequency cannot be less than 1600000." );
				return;
			}
		}

		var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();
		if( f < eval(region + "_FREQ_MIN") || f > eval(region + "_FREQ_MAX"))
		{
			alert("<%:Out of the frequency range of the selected region%>");
			return;
		}
		if(cur_freqs.length > 0 ){
			if(  f < cur_freqs[0] && (cur_freqs[cur_freqs.length-1] - f ) > MAX_FREQ_NUM * 0.2 ){
				alert("The difference from the existing frequency can not exceed "+MAX_FREQ_NUM * 0.2+"MHz");
				return;
			}

			if( f > cur_freqs[cur_freqs.length-1] && (f - cur_freqs[0] ) > MAX_FREQ_NUM * 0.2 ){
				alert("The difference from the existing frequency can not exceed "+MAX_FREQ_NUM * 0.2+"MHz");
				return;
			}
		}
		cur_stds.push({ freq: f, sf : dr, bw : bw});
		cur_stds.sort(function(a,b){
			return a.freq - b.freq;
		});

		init_lora_list(cur_stds);
		$("#std-freq-new").val("");
		caculate_radio_params()
	}
	function remove_std_freq(item){
		$(item).prev().val("");
		caculate_radio_params()
	}

	function add_fsk_freq(){
		var f = $("#fsk-freq-new").val();
		var dr = $("#fsk-dr-new").val();
		var cur_freqs = get_current_freqs();
		var cur_fsks = get_current_fsks();

		$("#fsk-freq-new").val("");
		if( cur_fsks.length >= SX1301_NUM ){
			
			return;
		}
		var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();

		for(var i = 0; i < cur_fsks.length; i++ ){
			if( Math.abs(cur_fsks[i].freq * 1000000 - f*1000000) < 1.6*1000000 ){
				alert("The difference between the new channel's frequency and the existing channel's frequency cannot be less than 1600000." );
				return;
			}
		}

		if( f < eval(region + "_FREQ_MIN") || f > eval(region + "_FREQ_MAX"))
		{
			alert("<%:Out of the frequency range of the selected region%>");
			return;
		}
		if( cur_freqs.length > 0 ){
			if( f < cur_freqs[0] && (cur_freqs[cur_freqs.length-1] - f ) > MAX_FREQ_NUM * 0.2 ){
				alert("The difference from the existing frequency can not exceed "+MAX_FREQ_NUM * 0.2+"MHz");
				return;
			}

			if( f > cur_freqs[cur_freqs.length-1] && (f - cur_freqs[0] ) > MAX_FREQ_NUM * 0.2 ){
				alert("The difference from the existing frequency can not exceed "+MAX_FREQ_NUM * 0.2+"MHz");
				return;
			}
		}
		cur_fsks.push({ freq: f, dr : dr});
		cur_fsks.sort(function(a,b){
			return a.freq - b.freq;
		});

		init_fsk_list(cur_fsks);

		caculate_radio_params();

	}

	function remove_fsk_freq(item){
		$(item).prev().val("");
		caculate_radio_params();
	}

	$(document).ready(function(){

		$("form").submit(function(e){
			console.log("###",$("input[name='cbid.lora_pkt_fwd.freq_plan.freq']").val())
			if( $("input[name='cbid.lora_pkt_fwd.freq_plan.freq']").val().length == "" ){
				$("input[name='cbid.lora_pkt_fwd.freq_plan.freq']").addClass("error");
				alert("<%:Please add at least one Multi-SF LoRa Channel!%>");
				return false;
			}
			
			return true;
		});

		var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();

		var tx_range = eval(region+"_TX_FREQ");

		$("#_tx_min").val(tx_range[0]*1000000);
		$("#_tx_max").val(tx_range[1]*1000000);
		if( region == "US915" || region == "AU915" || region == "CN470" || region == "KR920" ){
			$("#cbi-lora_pkt_fwd-freq_plan-fsk").hide();
		}
		else{
			//$("#cbi-lora_pkt_fwd-freq_plan-fsk").show();
			$("#fsk-dr-new").html("<option value='"+eval(region + "_FSK_DATARATE")+"' >"+eval(region + "_FSK_DATARATE") + "Kbps"+"</option>")
		}

		if( region == "CN470" || region == "IN865" || region == "KR920"){
			$("#cbi-lora_pkt_fwd-freq_plan-lora_std").hide();
		}
		else{
			//$("#cbi-lora_pkt_fwd-freq_plan-lora_std").show();
			$("#std-sf-new").html("<option value='"+eval(region + "_STD_SF" )+"' >SF" + eval(region + "_STD_SF") +"</option>");
			$("#std-bw-new").html("<option value='"+eval(region + "_STD_BW" )+"' >BW" + eval(region + "_STD_BW")+"</option>");
		}
		var cur_freqs = get_current_freqs();
		cur_freqs.sort(function(a,b){
			return a.freq - b.freq;
		});

		if(cur_freqs.length == 0){
			var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();
			var defs = eval(region + "_DEF_FREQS");
			defs.forEach(function(v){
				cur_freqs.push({freq:v, type:"multiSF"});
			});
		}

		init_freqlist(cur_freqs);
		caculate_radio_params();
		$("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").change(function(){
			var region = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region").val();
			var defs = eval(region + "_DEF_FREQS");
			var freqs = [];

			var tx_range = eval(region+"_TX_FREQ");

			$("#_tx_min").val(tx_range[0]*1000000);
			$("#_tx_max").val(tx_range[1]*1000000);

			defs.forEach(function(v){
				freqs.push({freq:v, type:"multiSF"});
			});

			init_freqlist(freqs);	
			init_lora_list([]);
			init_fsk_list([]);

			if( region == "US915" || region == "AU915" || region == "CN470" || region == "KR920" ){
				$("#cbi-lora_pkt_fwd-freq_plan-fsk").hide();
			}
			else{
				$("#cbi-lora_pkt_fwd-freq_plan-fsk").show();
				$("#fsk-dr-new").html("<option value='"+eval(region + "_FSK_DATARATE")+"' >"+eval(region + "_FSK_DATARATE") + "Kbps"+"</option>")
			}

			if( region == "CN470" || region == "IN865" || region == "KR920"){
				$("#cbi-lora_pkt_fwd-freq_plan-lora_std").hide();
			}
			else{
				$("#cbi-lora_pkt_fwd-freq_plan-lora_std").show();
				$("#std-sf-new").html("<option value='"+eval(region + "_STD_SF" )+"' >SF" + eval(region + "_STD_SF") +"</option>");
				$("#std-bw-new").html("<option value='"+eval(region + "_STD_BW" )+"' >BW" + eval(region + "_STD_BW")+"</option>");
			}
			caculate_radio_params();
			regChage();
			if( region == 'KR920'){
				$("#cbi-lora_pkt_fwd-freq_plan-lora_std").hide();
			}
		});

		$("#add_freq_btn").click(function(){
			var f = $("#freq-new").val();
			$("#freq-new").val("")
			add_freq(f);
		});
		
		

	});

	
</script>
<%+cbi/valueheader%>
<style type="text/css">
#freq-list-div{
	display: block;
	width: 100%;
	float:left;
}
#freq-list-div  i.fa{
	color:red;
	margin-left: -16px;
	display: none;
	cursor: pointer;
}
#freq-list-div > div:hover>i.fa{
	display:inline-block
}
#freq-list-div>div{
	float:left;
}

#freq-list-div > div> input{
	border:0;
	border-bottom:2px solid;
	width:120px;
	box-shadow: none;
	transition: box-shadow 0.2s line;
}
#freq-list-div > div> input[readonly]{
	background: transparent;
}
#freq-list-div > div> input.new{
	box-shadow: 0px 0px 4px #269cd4 inset;
}

#freq-list-div > div> input.error{
	box-shadow: 0px 0px 4px red inset;
}
</style>

<div id="freq-list-div">
	<%
	local vals = self:cfgvalue(section) or {}
	for i=1, #vals + 1 do
	local val = vals[i]
	if (val and #val > 0) or (i == 1) then
	%>
	<div>
		<input class="cbi-input-text" readonly value="<%=pcdata(val)%>" onchange="cbi_d_update(this.id)" type="text"<%= attr("name", cbid) .. ifattr(self.size, "size") ..
		ifattr(i == 1 and self.placeholder, "placeholder", self.placeholder)
		%> /><i onclick="remove_freq(this)" class="fa fa-close"></i></div>
		<% end end %>
	</div>

	<input type="number" step="0.0001" id="freq-new" style="width:120px" placeholder="<%:Freq.(MHz)%>" ><input type="button" id="add_freq_btn" value="<%:Add%>">
	<input type="hidden" id="_tx_min" name="_tx_min" />
	<input type="hidden" id="_tx_max" name="_tx_max" />
	<% for s=0,sx1301_nb - 1,1 do %>
	<input type="hidden" id="_<%=s%>_disabled" value="1" name="_<%=s%>_disabled" />
	<input type="hidden" id="_<%=s%>_radio0_enable" value="0" name="_<%=s%>_radio0_enable" />
	<input type="hidden" id="_<%=s%>_radio1_enable" value="0" name="_<%=s%>_radio1_enable" />
	<input type="hidden" id="_<%=s%>_radio0_center_freq" value="0" name="_<%=s%>_radio0_center_freq" />
	<input type="hidden" id="_<%=s%>_radio1_center_freq" value="0" name="_<%=s%>_radio1_center_freq" />
	<% for i=0,7,1 do %>
	<input type="hidden" id="_<%=s%>_ch<%=i%>_enable" value="0"  name="_<%=s%>_ch<%=i%>_enable" />
	<input type="hidden" id="_<%=s%>_ch<%=i%>_radio" value="0"  name="_<%=s%>_ch<%=i%>_radio" />
	<input type="hidden" id="_<%=s%>_ch<%=i%>_if" value="0" name="_<%=s%>_ch<%=i%>_if" />
	<%end%>

	<%end%>

	<%+cbi/valuefooter%>
